
/**
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 *
 * Description: Aircondition Sample Source. \n
 *
 * History: \n
 * 2023-04-03, Create file. \n
 */
#include "boards.h"
#include "pinctrl.h"
#include "gpio.h"
#include "soc_osal.h"
#include "app_init.h"
#include "sle_common.h"
#include "sle_car.h"
unsigned char g_local_addr[SLE_ADDR_LEN] = { 0x22, 0x22, 0x11, 0x11, 0x11, 0x11 };


#define AMBIENT_LIGHT_DURATION_MS        100

#define AMBIENT_LIGHT_TASK_PRIO          24
#define AMBIENT_LIGHT_TASK_STACK_SIZE    0x1000

sle_data_t g_sle_data={
    .type = NONE_TYPE,
    .data = {0,0,0,0} 
};


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pinctrl.h"
#include "pwm.h"
#include "tcxo.h"
#include "soc_osal.h"
#include "app_init.h"

#define PWM_CHANNEL 1
#define PWM_GROUP_ID 1
#define TEST_TCXO_DELAY_1000MS 1000

#define PWM_TASK_PRIO 24
#define PWM_TASK_STACK_SIZE 0x1000
#define CONFIG_PWM_PIN 9
#define CONFIG_PWM_PIN_MODE 1

static errcode_t pwm_sample_callback(uint8_t channel)
{
    osal_printk("PWM %d, cycle done. \r\n", channel);
    return ERRCODE_SUCC;
}
static void *pwm_task(const char *arg)
{
    UNUSED(arg);
    
    pwm_config_t cfg_no_repeat = {2000,  // 低电平时间
                                  g_ambient_light_britness, // 高电平持续tick 时间 = tick * (1/32000000)
                                  0,     // 相位偏移位
                                  0xFF,     // 发多少个波形
                                  true}; // 是否循环

    uapi_pin_set_mode(CONFIG_PWM_PIN, CONFIG_PWM_PIN_MODE);
    uapi_pwm_init();
    uapi_pwm_open(PWM_CHANNEL, &cfg_no_repeat);
    uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
    uapi_pwm_unregister_interrupt(PWM_GROUP_ID);
    uapi_pwm_register_interrupt(PWM_GROUP_ID, pwm_sample_callback);
    uint8_t channel_id = PWM_CHANNEL;
    uapi_pwm_set_group(PWM_GROUP_ID, &channel_id, 1); // 每个通道对应一个bit位
    uapi_pwm_start(PWM_GROUP_ID);
    while(1)
    {
        pwm_config_t cfg_no_repeat = {500,  // 低电平时间
                                  g_ambient_light_britness, // 高电平持续tick 时间 = tick * (1/32000000)
                                  0,     // 相位偏移位
                                  0xFF,     // 发多少个波形
                                  true}; // 是否循环
        uapi_pwm_open(PWM_CHANNEL, &cfg_no_repeat);
        uapi_pwm_start(PWM_GROUP_ID);
        printf("pwm_task g_ambient_light_britness: %d\r\n", g_ambient_light_britness);
        osal_msleep(50);
    }
    return NULL;
}

unsigned long hex_string_to_number(const char *hex_str) {
    unsigned long result = 0;
    int len = strlen(hex_str);

    for (int i = 0; i < len; i++) {
        // 将16进制字符转换为对应的数值
        result = result * 16 + (unsigned char)strtol(&hex_str[i], NULL, 16);
    }

    return result;
}

int g_ambient_light_britness = 0;
int msg_proc_main(char *hex_str) {
    // const char *hex_str = "1A3F";
    printf("Hex String: %s\r\n", hex_str);
    if (hex_str[0] == '5' && hex_str[1] == 'c') // LIGHT_COLOR_CHANGE
    {
        char value_str[3] = {0,0};
        sprintf(value_str,"%c%c",hex_str[6],hex_str[7]);
        printf("Hex value_str: %s\r\n", value_str);
        g_ambient_light_britness = hex_string_to_number(value_str);
        printf("g_ambient_light_britness: %lu\r\n", g_ambient_light_britness);
    }
    return 0;
}

static int ambient_light_task(const char *arg)
{
    unused(arg);
    osal_printk("ambient_light working.\r\n");
    
    while (1) {        
        msg_proc_main((char *)g_receive_msg);
    }

    return 0;
}

static void ambient_light_entry(void)
{
    osal_task *task_handle = NULL;
    osal_kthread_lock();
    task_handle = osal_kthread_create((osal_kthread_handler)ambient_light_task, 0, "AirconditionTask", AMBIENT_LIGHT_TASK_STACK_SIZE);
    if (task_handle != NULL) {
        osal_kthread_set_priority(task_handle, AMBIENT_LIGHT_TASK_PRIO);
        osal_kfree(task_handle);
    }
    osal_kthread_unlock();
}

/* Run the ambient_light_entry */
app_run(ambient_light_entry);

static void pwm_entry(void)
{
    osal_task *task_handle = NULL;
    osal_kthread_lock();
    task_handle = osal_kthread_create((osal_kthread_handler)pwm_task, 0, "PwmTask", PWM_TASK_STACK_SIZE);
    if (task_handle != NULL) {
        osal_kthread_set_priority(task_handle, PWM_TASK_PRIO);
        osal_kfree(task_handle);
    }
    osal_kthread_unlock();
}

/* Run the pwm_entry. */
app_run(pwm_entry);